/**
 * 
 */
package com.csair.youth.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.dao.impl.hibernate3.RoleDaoImpl;
import com.csair.youth.dao.impl.hibernate3.RolePermissionDaoImpl;
import com.csair.youth.database.Role;
import com.csair.youth.database.RolePermission;

/**
 * @author mickey
 *
 */
public class RoleServiceImpl {
	private RoleDaoImpl rdao;
	private RolePermissionDaoImpl rpdao;

	public RoleDaoImpl getRdao() {
		return rdao;
	}

	public void setRdao(RoleDaoImpl rdao) {
		this.rdao = rdao;
	}
	
	public RolePermissionDaoImpl getRpdao() {
		return rpdao;
	}

	public void setRpdao(RolePermissionDaoImpl rpdao) {
		this.rpdao = rpdao;
	}

	public void saveRole(Role item){
		if(item == null) return;
		if(StringUtils.isEmpty(item.getId())) item.setId(null);
		rdao.saveOrUpdate(item);
	}
	
	public void deleteRoleById(String id){
		if(!StringUtils.isEmpty(id)) rdao.deleteById(id);
	}
	
	public Role getRole(String id){
		return rdao.findById(id);
	}
	
	public List getRoles(){
		return rdao.findAllList();
	}
	
	public void saveRolePermissions(String roleId, String permissionIds[]){
		if(StringUtils.isNotEmpty(roleId)){
			if(permissionIds != null){
				for(int i=0; i<permissionIds.length; i++){
					if(StringUtils.isNotEmpty(permissionIds[i])){
						RolePermission rp = new RolePermission();
						rp.setRoleId(roleId);
						rp.setPermissionId(permissionIds[i]);
						rpdao.save(rp);
					}
				}
			}
		}
	}
	
	public void deleteRoleOldPermissions(String roleId){
		rpdao.deleteRoleOldPermissions(roleId);
	}
}
